// pages/indexAll/yiqiList/yiqiList.js
const app = getApp()
var self;
var nowpage = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowSelect: [],
    allSelect: [],
    selectIndex: -1,
    searchTxt: "",
    yiqiList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    self = this;
    nowpage = 1;
    self.setSelect();
  },
  getYiqiList: function() {
    //开始搜集搜索项
    var YiQiMingCheng = self.data.searchTxt;
    var YiQiFenLeiID = -999;
    var SuoZaiDanWei = -999;
    var DeviceOnlineStatus = -999;
    var JiaZhi = "";
    var tmparr = self.data.allSelect;
    for (var i = 0; i < tmparr[0].length; i++) {
      if (tmparr[0][i].checked) {
        DeviceOnlineStatus = tmparr[0][i].id;
      }
    }
    for (var i = 0; i < tmparr[1].length; i++) {
      if (tmparr[1][i].checked) {
        SuoZaiDanWei = tmparr[1][i].id;
      }
    }
    for (var i = 0; i < tmparr[2].length; i++) {
      if (tmparr[2][i].checked) {
        YiQiFenLeiID = tmparr[2][i].id;
      }
    }
    for (var i = 0; i < tmparr[3].length; i++) {
      if (tmparr[3][i].checked) {
        JiaZhi = tmparr[3][i].id;
      }
    }
    if (nowpage>1){
      wx.showLoading({
        title: '正在加载',
      })
    }
    
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetYiQiList",
      data: {
        YiQiMingCheng: YiQiMingCheng,
        YiQiFenLeiID: YiQiFenLeiID,
        SuoZaiDanWei: SuoZaiDanWei,
        DeviceOnlineStatus: DeviceOnlineStatus,
        JiaZhi: JiaZhi,
        rows: 4,
        page: nowpage
      },
      method: 'GET',
      success: function(res) {
        setTimeout(function(){
          wx.hideLoading();
        },500);
        
        if (res.data.state == 1) {

          var tmplist = self.data.yiqiList;
          var yiqiList = res.data.retData.YiQiList;
          if (yiqiList.length > 0) {
            nowpage++;
            for (var i = 0; i < yiqiList.length; i++) {
              if (yiqiList[i].YiQiAdmin.length > 8) {
                yiqiList[i].YiQiAdmin = yiqiList[i].YiQiAdmin.substring(0, 8) + "...";
              }
              tmplist.push(yiqiList[i]);
            }
            self.setData({
              yiqiList: tmplist
            })
          } else {
            wx.showToast({
              title: '暂无更多数据',
              icon: 'none'
            })
          }

        }
      }
    });
  },
  setSelect: function() {
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetJianSuoTiaoJianList",
      data: {},
      method: 'GET',
      success: function(res) {

        if (res.data.state == 1) {

          var arr1 = [];
          var tmp = res.data.retData.ZhuangTai;

          for (var i = 0; i < tmp.length; i++) {
            var obj = {};
            obj.id = tmp[i].DictionaryValueID;
            obj.name = tmp[i].DictionaryValueName;
            obj.checked = false;
            arr1.push(obj);
          }
          var arr2 = [];
          tmp = res.data.retData.DanWei;
          for (var i = 0; i < tmp.length; i++) {
            var obj = {};
            obj.id = tmp[i].YongHuZuZhiID;
            obj.name = tmp[i].YongHuZuZhiMingCheng;
            obj.checked = false;
            arr2.push(obj);
          }
          var arr3 = [];
          tmp = res.data.retData.FenLei;
          for (var i = 0; i < tmp.length; i++) {
            var obj = {};
            obj.id = tmp[i].YiQiFenLeiID;
            obj.name = tmp[i].FenLeiMingCheng;
            obj.checked = false;
            arr3.push(obj);
          }
          var arr4 = [];
          tmp = res.data.retData.JiaZhi;
          for (var i = 0; i < tmp.length; i++) {
            var obj = {};
            obj.id = tmp[i].ZiDianZhiID;
            obj.name = tmp[i].ZiDianZhiMingCheng;
            obj.checked = false;
            arr4.push(obj);
          }
          self.setData({
            allSelect: [arr1, arr2, arr3, arr4]
          });
          self.getYiqiList();
        }
      }
    });

  },
  changeTab: function(e) {
    self.setData({
      nowSelect: self.data.allSelect[e.currentTarget.dataset.index],
      selectIndex: e.currentTarget.dataset.index
    })
  },
  //选中标签
  selectTab: function(e) {

    var tmparr = self.data.allSelect;
    for (var i = 0; i < tmparr[self.data.selectIndex].length; i++) {
      if (i == e.currentTarget.dataset.index) {
        tmparr[self.data.selectIndex][i].checked = true;
      } else {
        tmparr[self.data.selectIndex][i].checked = false;
      }
    }

    var nowSelect = self.data.nowSelect;
    for (var i = 0; i < nowSelect.length; i++) {
      if (i == e.currentTarget.dataset.index) {
        nowSelect[i].checked = true;
      } else {
        nowSelect[i].checked = false;
      }
    }
    self.setData({
      nowSelect: nowSelect,
      allSelect: tmparr
    })

  },
  resetSearch: function() {
    var tmparr = self.data.allSelect;
    for (var i = 0; i < tmparr.length; i++) {
      for (var j = 0; j < tmparr[i].length; j++) {
        tmparr[i][j].checked = false;
      }
    }

    var nowSelect = self.data.nowSelect;
    for (var i = 0; i < nowSelect.length; i++) {
      nowSelect[i].checked = false;
    }
    self.setData({
      nowSelect: nowSelect,
      allSelect: tmparr
    });

  },
  closeSelect: function() {
    self.setData({
      selectIndex: -1
    })
  },
  changeTxt: function(e) {
    self.setData({
      searchTxt: e.detail.value
    })
  },
  goSearch() {
    self.closeSelect();
    nowpage = 1;
    self.setData({
      yiqiList: []
    })
    self.getYiqiList();

  },
  goYiqiDetail: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/indexAll/yiqiDetail/yiqiDetail?id='+e.currentTarget.dataset.id,
    })
  },
  goYiqiStep: function (e) {
    wx.navigateTo({
      url: '/pages/indexAll/yiqiStep1/yiqiStep1?id=' + e.currentTarget.dataset.id,
    })
  },
  goSyOp:function(e){
    wx.navigateTo({
      url: '/pages/indexAll/songyangStep1/songyangStep1?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    self.getYiqiList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})